package edu.wfu.preparedDishes.order.entity.vo;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import edu.wfu.preparedDishes.order.entity.po.SkuNum;
import io.renren.modules.order.enity.OrderInfo;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

@Data
@Accessors(chain = true)
public class MerchantOrderEntityVo {
    /**
     * 订单编号
     */
    private String orderCode;
    /**
     * 订单状态
     */
    private Integer orderStatus;
    /**
     * 订单总价
     */
    private Integer orderTotalPrice;
    /**
     * 收货地址
     */
    private String address;
    /**
     * 详细地址
     */
    private String fullAddress;
    /**
     * 最晚发货时间
     */
    private String sendTime;
    /**
     * 备注
     */
    private String remark;
    /**
     * 收货人
     */
    private String receiver;
    /**
     * 收货人电话
     */
    private String receiverPhone;
    /**
     * 每个商品的数量
     */
    private String skuNumbers;

    private List<SkuNum> skuNums;

    private OrderInfo orderInfo;

    /* @Data
     public static class SkuNum{
         private String sku_id;
         private Integer num;
         private OrderInfo orderInfo;
         public String toJsonString() throws JsonProcessingException {
             try {
                 ObjectMapper objectMapper = new ObjectMapper();
                 return objectMapper.writeValueAsString(this);
             }catch (Exception e) {
                 e.printStackTrace();
                 return null;
             }
         }
     }*/
    /*@Data
    public static class OrderInfo{
        private String spuName;
        private String skuSpecs;
        public String toJsonString() throws JsonProcessingException {
            try {
                ObjectMapper objectMapper = new ObjectMapper();
                return objectMapper.writeValueAsString(this);
            }catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    }*/
    public static List<SkuNum> fromJsonStringToOrderNum(String jsonString) throws IOException {
        if (jsonString==null || jsonString.isEmpty()) {
            return Collections.emptyList();
        }
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return (objectMapper.readValue(jsonString, new TypeReference<List<SkuNum>>() {
            }));
        }catch (JsonProcessingException e){
            SkuNum singleSkuNum=objectMapper.readValue(jsonString, SkuNum.class);
            return Collections.singletonList(singleSkuNum);
        }
    }
    public static OrderInfo fromJsonStringOrderInfo(String jsonString) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.readValue(jsonString, OrderInfo.class);
    }

}
